const puppeteer =  require("puppeteer");
const cheerio = require("cheerio");
var detail = async ctx =>{
    var {id} = ctx.request.query;
    var url = `https://music.163.com/#/playlist?id=${id}`
    // console.log(id);
    const browser = await puppeteer.launch({
        // headless:false,
        defaultViewport:null
    });
    const page = await browser.newPage();
    await page.goto(url);
    // await page.waitForTimeout(3000)
    // 找到iframe
    var iframe = await page.$("#g_iframe");
    // contentFrame获取iframe中的html
    var frame = await iframe.contentFrame();
    // 抓取
    var content = await frame.content();
    const $ = cheerio.load(content,{
        decodeEntities:false
    })
    let arr={};
    let mainImg = $(".m-info").find(".cover img").attr("src");
    let mainTitle = $(".m-info").find(".tit .f-brk").html();
    let mainIntroduce = $("#album-desc-more").text();
    let comment = []
    $(".itm").each((index,value)=>{
        let cri = $(value).find(".cnt").html().split("</a>：")[1];
        comment.push(cri);
    })
    Object.assign(arr,{
        mainImg,
        mainTitle,
        mainIntroduce,
        comment
    })
    ctx.body = {
        arr
    }
}
module.exports = detail;